//
//  CardViewController.m
//  Toyohu
//
//  Created by Heige on 16/4/15.
//  Copyright © 2016年 celia. All rights reserved.
//

#import "CardViewController.h"
#import "masonry.h"
#import "GPCustomMD.h"
#import "HPChildren.h"
#import "HPViewModel.h"
#import "GPAFNetworking3.h"
#import "HPTask.h"
#import <ReactiveCocoa.h>
#import "VerticallyAlignedLabel.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height


@interface CardViewController ()<UIScrollViewDelegate>
{
    NSMutableArray *topArray;
    NSMutableArray *downArray;
    NSMutableArray *imgArray;
    NSInteger number;
    UIView *view;
    NSInteger offset;
    UIButton *myButton;
    
    NSMutableArray *_successArray;
    
}

@end

@implementation CardViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _successArray = [[NSMutableArray alloc] initWithCapacity:0];
    
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[[UIImage imageNamed:@"Cradbg"]imageWithRenderingMode:UIImageRenderingModeAutomatic]];
    self.scrollView = [[HJTCardView alloc]initWithFrame:CGRectMake(0, 0, 200, 400)];
    self.scrollView.center = CGPointMake(kScreenW/2, 300);
    
    
    [self.view addSubview:self.scrollView];
    self.scrollView.effect = HJTCardViewEffectCarousel;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    
    
    myButton = [UIButton buttonWithType:UIButtonTypeSystem];
    myButton.frame = CGRectMake(0, 0, 104, 50);
    
    myButton.layer.borderWidth = 2;
    myButton.layer.cornerRadius = 25;
    myButton.clipsToBounds = YES;
    
    [myButton setTintColor:[UIColor cyanColor]];
    myButton.layer.borderColor = [UIColor cyanColor].CGColor;
    [myButton setBackgroundColor:[UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1]];
    [myButton addTarget:self action:@selector(DoneTask) forControlEvents:UIControlEventTouchDown];
    myButton.center = CGPointMake(kScreenW/2, kScreenH-100);
    [myButton setTitle:@"完成任务" forState:UIControlStateNormal];
    
    if (self.tasks.count) {
        if (((HPTask *)[self.tasks objectAtIndex:0]).isDone == 1) {
            myButton.alpha = 0.5;
            myButton.enabled = NO;
            [myButton setTitle:@"已完成" forState:UIControlStateNormal];
            //myButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        }
        myButton.layer.borderColor = [UIColor cyanColor].CGColor;
    }
    
    
    [self.view addSubview:myButton];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(15, 30, 22, 22);
    [backButton setBackgroundImage:[UIImage imageNamed:@"cardreturn@3x"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backPreviousPage) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:backButton];
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [shareButton addTarget:self action:@selector(doShare) forControlEvents:UIControlEventTouchDown];
    [shareButton setBackgroundImage:[UIImage imageNamed:@"cardshare@2x"] forState:UIControlStateNormal];
    [self.view addSubview:shareButton];
    [shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@22);
        make.height.equalTo(@21);
        make.centerY.equalTo(backButton.mas_centerY);
        make.left.equalTo(backButton.mas_right).offset(20);
    }];
    
    topArray = [NSMutableArray arrayWithObjects:_myTitle, nil];
    downArray = [NSMutableArray arrayWithObjects:_myContent, nil];
    imgArray = [NSMutableArray arrayWithObjects:@"green@2x",@"yellow@2x",@"purple@2x", nil];
    for (int i = 0; i < _taskCout; i ++) {
        [self createCardWithColorAndtopTitle:topArray downTitle:downArray count:i imgs:imgArray];
    }
}


-(void)backPreviousPage
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
    if (self.successBlock) {
        self.successBlock(_successArray);
    }
    
}

-(void)doShare
{
    
}
//封装上下内容 以及背景图的设置方法
- (void)createCardWithColorAndtopTitle:(NSMutableArray *)TopArray downTitle:(NSMutableArray *)DownArray count:(NSInteger)count imgs:(NSMutableArray *)Imgs
{
    CGFloat width = CGRectGetWidth(self.scrollView.frame);
    CGFloat height = CGRectGetHeight(self.scrollView.frame);
    
    CGFloat x = self.scrollView.subviews.count * width;
    
    view = [[UIView alloc] initWithFrame:CGRectMake(x, 0, 240, 288)];
    view.layer.cornerRadius = 8.;
    UIImageView *pinkyView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 29, 22, 22)];
    pinkyView.image = [UIImage imageNamed:@"done@2x"];
    view.tag = count+200;
    [view addSubview:pinkyView];
    
    UILabel *topLabel = [[UILabel alloc]init];
    topLabel.numberOfLines = 0;
    topLabel.textColor = [UIColor whiteColor];
    [view addSubview:topLabel];
    [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(pinkyView.mas_right).offset(10);
        make.centerY.equalTo(pinkyView.mas_centerY);
        make.width.equalTo(@(180));
        make.height.equalTo(@(50));
    }];
    
    VerticallyAlignedLabel*downLabel = [[VerticallyAlignedLabel alloc]initWithFrame:CGRectMake(0, 0, 220, 205)];
    downLabel.numberOfLines = 0;
    downLabel.center = CGPointMake(view.frame.size.width/2, 173);
    downLabel.verticalAlignment = VerticalAlignmentTop;
    downLabel.textColor = [UIColor lightGrayColor];
    downLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:downLabel];
    
    view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:Imgs[count]]];
    for (int i = 0; i < _taskCout; i++) {

        HPTask *model = _tasks[count];
        
        topLabel.text = model.tTitle;
        downLabel.text = model.tDescribe;
       
    }
    
    
    [self.scrollView addSubview:view];
    self.scrollView.contentSize = CGSizeMake(x + width, height);

    
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 计算当前在第几页
    offset =  (NSInteger)(self.scrollView.contentOffset.x / 200);
    if (((HPTask *)[self.tasks objectAtIndex:offset]).isDone == 1) {
        myButton.alpha = 0.5;
        myButton.enabled = NO;
        [myButton setTitle:@"已完成" forState:UIControlStateNormal];
    }else{
        myButton.alpha = 1;
        myButton.enabled = YES;
        [myButton setTitle:@"完成任务" forState:UIControlStateNormal];
    }
    
}

-(void)DoneTask
{
    //通过UIScrollViewDelegate协议找到对应的View
    if (offset==0) {
        UIView *myView = [self.view viewWithTag:200];
        myView.backgroundColor = [UIColor lightGrayColor];
        
    }
    else if (offset==1)
    {
        UIView *myView = [self.view viewWithTag:201];
        myView.backgroundColor = [UIColor lightGrayColor];
    }
    else if (offset==2)
    {
        UIView *myView = [self.view viewWithTag:202];
        myView.backgroundColor = [UIColor lightGrayColor];
    }

     NSString *pid = [[(NSDictionary *)GPGET(@"loginInfo") objectForKey:@"msg"] objectForKey:@"pId"];
   
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
   
    
    [GPAFNetworking3 post:FinishTasks parameters:@{@"cId":@(_cid),@"tDoneDate":dateString,@"pId":pid,@"tId":@(_tid),@"tlPictureUrl":@"",@"tlDescribe":@"",@"tlId":@(0)} progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nullable responseObject) {
        
        if ([[responseObject objectForKey:@"code"] floatValue] == 1) {
            [_successArray addObject:@(offset)];
            [SVProgressHUD showSuccessWithStatus:@"已完成"];
        }else{
            SHOW_ERROR
        }
        
        
        
         //[self dismissViewControllerAnimated:YES completion:nil];
        
    } failure:^(NSString * _Nonnull errorLD) {
//        NSLog(@"%@",errorLD);

        SHOW_NTERROR
    }];
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
